package com.bajiao.app.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.HashMap;
import java.util.Map;

public class VXCodeGener {

    public static void main(String[] args) {
        getAppletUrl();
    }

    public static String getWXToken() {
        //小程序APPID
        String appid="wx731003e8517fe553";//自行获取
        //小程序secret
        String secret="0b9e5d2d400c670d3df8da3a1df6dfcd";
        String httpUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
        httpUrl= httpUrl+"&appid="+appid+"&secret="+secret;
        HttpResponse response = HttpRequest.get(httpUrl)
                .execute();
        System.out.println(response);
//
        JSONObject jsonObject = JSONUtil.parseObj(response.body());
        String accessToken = jsonObject.getStr("access_token");
        return accessToken;
    }


    //生成小程序跳转链接
    public static Map<String,Object> getAppletUrl(){
        Map<String,Object> map=new HashMap<String,Object>();
        //接口地址
        String httpUrl="https://api.weixin.qq.com/wxa/generatescheme?access_token=";
        //需要跳转的小程序路径
        String path= "pages/tabbar/home/index";
        //获取AccessToken
        String AccessTokenJson=getWXToken();
        //格式化返回数据
            map.put("code",0);
            try {
                String content="";
                JSONObject jsonParam = new JSONObject();
                JSONObject jump_wxa = new JSONObject();
                jump_wxa.put("path",path);
                jump_wxa.put("query", "");
                jsonParam.put("jump_wxa",jump_wxa);
                jsonParam.put("expire_type", 1);
                jsonParam.put("expire_interval", 2);
                String params=jsonParam.toString();
                CloseableHttpClient httpClient = HttpClients.createDefault();
                RequestConfig requestConfig = RequestConfig.custom()
                        .setSocketTimeout(300 * 1000)
                        .setConnectTimeout(300 * 1000)
                        .build();
                HttpPost post = new HttpPost(httpUrl+AccessTokenJson);
                post.setConfig(requestConfig);
                post.setHeader("Content-Type", "application/json;charset=utf-8");
                //URLEncoder.encode(name)
                StringEntity postingString = new StringEntity(params, "utf-8");
                post.setEntity(postingString);
                CloseableHttpResponse response = httpClient.execute(post);
                content= EntityUtils.toString(response.getEntity());
                JSONObject resultUrl = JSONUtil.parseObj(content);
                String newUrl=resultUrl.get("openlink").toString();
                map.put("url",newUrl);
                System.out.println(newUrl);
            } catch (SocketTimeoutException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        return map;
    }

}
